import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:freemusic/components/Common.dart';
import 'package:freemusic/page/other/AboutPage.dart';
import 'package:freemusic/services/SongServices.dart';
import 'package:freemusic/utils/Util.dart';
import 'package:url_launcher/url_launcher.dart';

class DrawerPage extends StatelessWidget {
  void _handleHelpOnTap(BuildContext context) {
    showCommonDialog(
      context,
      title: "反馈与建议",
      content: "如果您对这款APP有更好的想法，欢迎进群交流！点击确定跳转到手机QQ\nQQ群：773464316",
      onConfirm: () {
        Navigator.of(context).pop();
        launch("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3DbMFKxi0XycVuLxq5S5MPL4UHyzM9yw0h");
      },
    );
  }

  void _handleCleanOnTap(){
    SongServices.getInstace().cleanLocalCache().then((v){
      if(v){
        Fluttertoast.showToast(msg: "清理成功");
      }
    });
  }

  void _handleAboutOnTap(BuildContext context){
    Navigator.of(context).push(MaterialPageRoute(builder: (context) => AboutPage(), ));
  }

  //flutter 与 android 底层通信
  void _handleJoinGroupOnTap(){
    final platform = MethodChannel("com.jt.freemusic/flutter");
    platform.invokeMethod("joinGroup").then((v){
      if(!v){
        Fluttertoast.showToast(msg: "手机QQ打开失败");
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    Color themeColor = Util.themeColor;
    Color white = Colors.white;
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(left: 12),
            height: 180,
            decoration: BoxDecoration(color: themeColor),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.end,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  padding: EdgeInsets.only(
                    top: 8,
                  ),
                  child: Text(
                    "FreeMusic",
                    style: TextStyle(color: white, fontSize: 32),
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(top: 8, bottom: 8),
                  child: Text(
                    "集多平台于一身的便捷、实用听歌APP",
                    style: TextStyle(
                      color: white,
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            padding: EdgeInsets.only(top: 12),
            child: Column(
              children: <Widget>[
                _DrawerButton(
                  title: "清理缓存",
                  image: "images/qingli.png",
                  onTap: this._handleCleanOnTap,
                ),
                _DrawerButton(
                  title: "进群交流",
                  image: "images/qun.png",
                  onTap: this._handleJoinGroupOnTap,
                ),
                _DrawerButton(
                  title: "反馈与建议",
                  image: "images/fankui.png",
                  onTap: () {
                    this._handleHelpOnTap(context);
                  },
                ),
                _DrawerButton(
                  title: "关于",
                  image: "images/guanyu.png",
                  onTap: () {
                    this._handleAboutOnTap(context);
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class _DrawerButton extends StatelessWidget {
  final String image;
  final String title;
  final Function onTap;

  const _DrawerButton(
      {Key key, @required this.image, @required this.title, this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: this.onTap,
      child: Container(
        padding: EdgeInsets.only(left: 12.0, right: 12.0),
        height: 48,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(right: 24),
              child: Image.asset(
                this.image,
                width: 20,
              ),
            ),
            Container(
              child: Text(
                this.title,
                style: TextStyle(fontSize: 16),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
